﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
using System.Configuration;
using ICSharpCode.SharpZipLib;

public partial class Candidate_Completed_Workflow : System.Web.UI.Page
{

    Guid gudUserID = CommonUtility.GetLoggedOnUserId();


    protected void Page_Load(object sender, EventArgs e)
    {
        txtDateFrom.Attributes.Add("readonly", "readonly");
        txtDateUpto.Attributes.Add("readonly", "readonly");
        if (!IsPostBack)
        {
            gvActivity.Visible = false;
            //txtDateFrom.Text = DateTime.Today.ToString("dd/MM/yyyy");
            //txtDateUpto.Text = DateTime.Today.ToString("dd/MM/yyyy");
            ddlvalue.Visible = false;


            GetCountry();

            // Session["UserID"] = Guid.NewGuid();
            GetRegion();
        }
        

    }

    private DataSet GetDataSet()
    {
        DataSet objDs = null;
        try
        {
            objDs = new CandidateWorkflowBAL().GetCandidateWorkflow(ddlEmail.SelectedValue,txtvalue.Text.Trim(),ddlCountry.SelectedValue.ToInt(), CommonUtility.ConvertDateyyyymmdd(txtDateFrom.Text), CommonUtility.ConvertDateyyyymmdd(txtDateUpto.Text), ddlRegion.SelectedValue.ToInt(), 1);
            btnDownload.Visible = false;
            if (objDs != null)
            {
                if (objDs.Tables.Count > 0)
                {
                    if (objDs.Tables[0].Rows.Count > 0)
                    {
                        btnDownload.Visible = true;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = "Error : " + ex.Message;
            ex.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;


    }

    
    protected void BtnSearch_OnClick(object sender, EventArgs e)
    {
        try
        {
            gvActivity.Visible = true;
            
            hdnInput.Value = "search";
            GetData();
            Clear();
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = "Error : " + ex.Message;
            //ex.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }

    }
    private void Clear()
    {
        lblErrorMessage.Text = string.Empty;
    }
    protected void BtnClear_OnClick(object sender, EventArgs e)
    {
        try
        {
            gvActivity.Visible = false;
            btnDownload.Visible = false;
            ddlEmail.ClearSelection();
            ddlCountry.ClearSelection();
            ddlRegion.ClearSelection();
            lblvalue.Text = string.Empty;
            //txtvalue.Visible = false;
            ddlvalue.Visible = false;
            txtvalue.Text = string.Empty;
            //txtState.Text = string.Empty;
            txtDateFrom.Text = string.Empty;
            txtDateUpto.Text = string.Empty;
            lblErrorMessage.Text = string.Empty;

            //ddlState.SelectedValue = string.Empty;
            hdnInput.Value = string.Empty;
            //txtLanguageId.Text = "";
            //GetData();
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = "Error : " + ex.Message;
        }
    }

    private void GetCountry()
    {
        try
        {
            CountryMasterBAL objBAL = new CountryMasterBAL();
            DataSet ds = objBAL.GetCountry();
            if (ds != null)
            {
                ddlCountry.DataSource = ds;
                ddlCountry.DataTextField = "CountryName";
                ddlCountry.DataValueField = "CountryID";
                ddlCountry.DataBind();
                ddlCountry.Items.Insert(0, new ListItem("--Select Country--", "0"));
            }
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = "Error : " + ex.Message;
        }
    }

    private void GetRegion()
    {
        RegionMasterBAL objBAL = new RegionMasterBAL();
        DataSet ds = objBAL.GetRegionMaster();
        if (ds != null)
        {
            ddlRegion.DataSource = ds;
            ddlRegion.DataTextField = "RegionName";
            ddlRegion.DataValueField = "RegionID";
            ddlRegion.DataBind();
            ddlRegion.Items.Insert(0, new ListItem("--Select Region--", "0"));
        }
    }


    protected void gvActivity_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    //protected void gvActivity_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        LinkButton l = (LinkButton)e.Row.FindControl("lnkDelete");
    //        if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "IsActive")) == 0)
    //        {
    //            l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to active " + DataBinder.Eval(e.Row.DataItem, "ActivityName") + " Activity?')");
    //        }
    //        else
    //        {
    //            l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete " + DataBinder.Eval(e.Row.DataItem, "ActivityName") + " Activity?')");
    //        }
    //    }

    //}
    //protected void gvActivity_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    int intActivityID = (int)gvActivity.DataKeys[e.RowIndex].Value;
    //    string strStatus = Convert.ToString(gvActivity.Rows[e.RowIndex].Cells[4].Text).ToUpper() == "ACTIVE" ? "DELETE" : "ACTIVE";
    //    DeleteActivity(intActivityID, strStatus);
    //    GetData();

    //}


    private void GetData(params object[] param)
    {
        try
        {
            if (hdnInput.Value == "search")
            {
                DataSet objDs = new DataSet();
                objDs = GetDataSet();
                gvActivity.DataSource = objDs;
                gvActivity.DataBind();
            }
            else
            {
                gvActivity.DataSource = GetDataSet();
                gvActivity.DataBind();
            }
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = "Error : " + ex.Message;
        }
        
    }


    protected void gvActivity_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvActivity.PageIndex = e.NewPageIndex;
            GetData();
            Clear();
        }
        catch(Exception ex)
        {
            lblErrorMessage.Text = "Error : " + ex.Message;
        }


        
    }
    protected void txtActivityName_TextChanged(object sender, EventArgs e)
    {

    }
    private void DeleteActivity(int intActivity, string strSetMethod)
    {
        ActivityMasterBAL objAMBal;
        ActivityMasterBO objAMBO;
        //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
        string strmod = Convert.ToString(Request.QueryString["mod"]);
        string strReturnValue;


        objAMBal = new ActivityMasterBAL();
        objAMBO = new ActivityMasterBO();

        objAMBO.SetMethod = strSetMethod;
        objAMBO.ActivityID = intActivity;
        objAMBO.ModifyBy = gudUserID;
        objAMBO.ModifyDate = System.DateTime.Today;

        objAMBO.IsActive = (strSetMethod == "ACTIVE" ? true : false);
        strReturnValue = (string)objAMBal.SetActivityMaster(objAMBO);
        if (strReturnValue == "SUCCESS")
        {
            lblErrorMessage.Text = "Record has been updated succesfully.";
            lblErrorMessage.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            lblErrorMessage.Text = "Error while saving record.";
            lblErrorMessage.ForeColor = System.Drawing.Color.Red;
        }
    }
    protected void ddlEmail_SelectedIndexChanged(object sender, EventArgs e)
    {

        try
        {
            if (ddlEmail.SelectedIndex == 0)
            {
                ddlvalue.Visible = false;
                lblvalue.Visible = false;
            }
            else if (ddlEmail.SelectedIndex == 1)
            {
                ddlvalue.Visible = true;
                lblvalue.Visible = true;
                txtvalue.Text = string.Empty;
                lblvalue.Text = ddlEmail.SelectedItem.Text.Trim() + ":";
                //ddlEmail.SelectedValue =  ddlEmail.SelectedItem.Text.Trim();
            }
            else if (ddlEmail.SelectedIndex == 2)
            {
                ddlvalue.Visible = true;
                lblvalue.Visible = true;
                txtvalue.Text = string.Empty;
                lblvalue.Text = ddlEmail.SelectedItem.Text.Trim() + ":";
                //ddlEmail.SelectedValue = ddlEmail.SelectedItem.Text.Trim();
            }
            else if (ddlEmail.SelectedIndex == 3)
            {
                ddlvalue.Visible = true;
                lblvalue.Visible = true;
                txtvalue.Text = string.Empty;
                lblvalue.Text = ddlEmail.SelectedItem.Text.Trim() + ":";
            }
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = "Error : " + ex.Message;
        }
            

        }
        
        
    
    protected void btn_download_Click(object sender, EventArgs e)
    {
        string filePath = ConfigurationManager.AppSettings["filePathExcel"];
        string parameter = null;
        string DATESTRINGFORMAT = "dd-MM-yyyy_HH-mm-ss";
        string SAMPLEFILENAME = "CandidateWorkFlow.xls";
        String ServerFileFolderPath = "";
        string Filename = SAMPLEFILENAME.Replace(".xls", "_" + "__" + DateTime.Now.ToString(DATESTRINGFORMAT)) + ".xls";
        try
        {
            DataSet objDs = new DataSet();
            objDs = GetDataSet();

            //CommonUtility.ErrorLog_Report("Candidate Work Flow", CommonUtility.GetLoggedOnUserId(), CommonUtility.GetLoggedOnUserName(), Filename, parameter);

            String SavedFileFolderPath = filePath + SAMPLEFILENAME.Replace(".xls", "_" + "__" + DateTime.Now.ToString(DATESTRINGFORMAT)) + ".xls";
            ServerFileFolderPath = Server.MapPath(SavedFileFolderPath);
            CommonUtility.ExportToExcel_Sheet(objDs, ServerFileFolderPath);
        }
    
        catch (Exception ex)
        {
            lblErrorMessage.Text = "Error : " + ex.Message;
        }
       
    }
}
